/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Nov 1, 2013 10:26:24 AM
 */
package com.googlecode.backtest.rule.queue;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.backtest.controller.IBackTestController;
import com.googlecode.backtest.controller.IConstructorArguments;
import com.googlecode.backtest.dataset.IBackTestStockDataset;
import com.googlecode.backtest.rule.IAlgorithm;
import com.googlecode.backtest.rule.ITAParameters;

/**
 * @author lianyuy
 * 
 */
public class ForLoopQueue
		extends
		ForLoopAlgorithmQueue<IBackTestStockDataset, ITAParameters, IAlgorithm<IBackTestStockDataset, ITAParameters>, Double> {
	private IBackTestStockDataset answer = null;
	protected List<Class<IAlgorithm<IBackTestStockDataset, ITAParameters>>> queue = null;

	/**
	 * @param start
	 * @param stop
	 * @param step
	 * @param controller
	 */
	public ForLoopQueue(
			Double start,
			Double stop,
			Double step,
			IBackTestController<IBackTestStockDataset, ITAParameters, IAlgorithm<IBackTestStockDataset, ITAParameters>> controller) {
		super(start, stop, step, controller);
		queue = new ArrayList<Class<IAlgorithm<IBackTestStockDataset, ITAParameters>>>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.rule.IAlgorithmQueue#getSize()
	 */
	@Override
	public int getSize() {
		if (queue != null)
			return queue.size();
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.rule.IAlgorithmQueue#getAlgorithm(int)
	 */
	@Override
	public Class<IAlgorithm<IBackTestStockDataset, ITAParameters>> getAlgorithm(
			int index) {
		return queue.get(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.rule.IBackTestAlgorithm#calculate(T[],
	 * java.lang.Object)
	 */
	@Override
	public boolean calculate(List<IBackTestStockDataset> dataset) {
		IBackTestStockDataset result = null;
		IConstructorArguments arguments = null;
		for (double v = getStart().doubleValue(); v <= getStop().doubleValue(); v = v
				+ getStep().doubleValue()) {
			List<IBackTestStockDataset> data = dataset;
			for (int i = 0; i < getSize(); i++) {
				Class<IAlgorithm<IBackTestStockDataset, ITAParameters>> classname = getAlgorithm(i);
				// TODO: Fixed
				// result = controller.runAlgorithm(classname, arguments, data,
				// parameter);
				data = new ArrayList<IBackTestStockDataset>();
				data.add(result);
			}
		}
		answer = result;

		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.rule.IAlgorithm#getResult()
	 */
	@Override
	public IBackTestStockDataset getResult() {
		return answer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.backtest.rule.GUIDable#getGUID()
	 */
	@Override
	public String getGUID() {
		// TODO Auto-generated method stub
		return null;
	}

}
